<template>
	<view class="pd16_15">
		<view class="box pd16_15 mb10"  v-if="mendiandatatype==1" v-for="(item,index) in addrs" :key="index"  @click="selectMendian(item.bus_id,item)">
			<view class="flex alcenter space">
				<image :src="item.avatar" mode="aspectFill" style="width: 100rpx;height: 100rpx;border-radius: 50%;"></image>
				<text class="ft16 ftw600 cl-main">{{item.nickname}}</text>
				<text v-if="item.distancea>0" class="cl-orange ft12">距您 {{(item.distancea/1000).toFixed(2)}}km</text>
			</view>
			<view class="flex alcenter  ft16 ftw600 cl-main" style="text-align: center;margin: 20rpx auto 0;justify-content: center;">地址：{{item.address}}</view>
			<view class="flex alcenter  ft16 ftw600 cl-main" style="text-align: center;margin: 20rpx auto 0;justify-content: center;">电话：{{item.phone}}</view>
		</view>
		
		<uni-load-more :status="status" :content-text="contentText" />
	</view>
</template>

<script>
	export default{
		data(){
			return {
				mrmendian:[],
				mendiandata:[],
				mendiandatatype:1,
				addrs:[],
				listData: [],
				last_id: 0,
				reload: true,
				status: 'more',
				statusa: false,
				text:'',
				keytext:'',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有数据了'
				},
				position:[],
			}
		},
		onLoad(e){
			if(uni.getStorageSync("position")){
				this.position=uni.getStorageSync("position")
			}else{
				uni.getLocation({
				    type: 'wgs84',
					geocode:true,
				    success: function (res) {
						this.position=res;
						uni.setStorage({//缓存配置信息
							key: 'position',  
							data: res
						})
				    }
				});
			}
		},
		onShow() {
			this.last_id=0
			this.reload=true
			this.getList();
		},
		// 监听原生标题导航按钮点击事件
		onNavigationBarButtonTap(e) {
			console.log(e.index)
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
				if(e.index==0){
					this.keytext='';
					this.last_id=0
					this.reload= true
					this.getList();
				}
		},
		onPullDownRefresh(){
			var this_=this
			uni.getLocation({
			    type: 'wgs84',
				geocode:true,
			    success: function (res) {
					console.log('刷新点位地址成功')
					uni.setStorage({//缓存配置信息
						key: 'position',  
						data: res
					})
					uni.stopPullDownRefresh();
					this_.last_id=0
					this_.reload=true
					this_.getList();
			    }
			});
		},
		onReachBottom() {
			this.status = 'more';
			this.getList();
		},
		//监听搜索框文本变化
		onNavigationBarSearchInputChanged(e) {
			let text = e.text;
			if(text){
				this.keytext=text;	
			}
			console.log(text)
		},
		//监听点击搜索按钮事件
		onNavigationBarSearchInputConfirmed(e) {
			let text = e.text;
			if (!text) {
				uni.showModal({
					title: '',
					content:"请输入搜索内容",
					showCancel: false,
					confirmText: "确定",
					confirmColor:"#e19503",
					success: function (res) {
						
					}
				});
				return;
			} else {
				this.keytext=text;
				this.last_id=0
				this.reload=true
				this.getList();
			}
			// #ifdef APP-PLUS
			plus.key.hideSoftKeybord();
			// #endif
			
		},
		methods:{
			telip(tel){
				uni.makePhoneCall({
				    phoneNumber: tel //仅为示例
				});
			},
			opdt(item){
				console.log(item.latitude)
				uni.openLocation({
				    latitude: parseFloat(item.latitude),
				    longitude: parseFloat(item.longitude),
					name: item.mname,
				    success: function () {
				        console.log('success');
				    }
				});
			},
			getList() {
				
				let data = { };
				if(this.position.latitude && this.position.longitude){
					data.latitude=this.position.latitude
					data.longitude=this.position.longitude
					var limit=1000;
				}else{
					var limit=10;
				}
				if(this.keytext){
					data.text=this.keytext
				}
				if (this.last_id>0) {
					//说明已有数据，目前处于上拉加载
					this.status = 'loading';
					data.offset = this.last_id*limit;
					data._ = new Date().getTime() + '';
				}
				
				if(this.position.address){
					data.province=this.position.address.province
					data.city=this.position.address.city
					data.district=this.position.address.district
				}
				if(this.text){
					data.text=this.text
				}
				data.limit=limit		
				console.log(this.position)
				this.$ajax('post','index/bus_list',data).then(res=>{
					this.addrs=res.data
				})
				// uni.request({
				// 	url: this.configs.webUrl+'index/bus_list',
				// 	data: data,
				// 	success: data => {
				// 		console.log(data.data)
				// 		if (data.data.total>0) {
				// 			this.mendiandatatype=1
				// 			let list = data.data.rows;
				// 			this.mrmendian=data.data.mrmendian
				// 			this.addrs = this.reload ? list : this.addrs.concat(list);
				// 			this.reload = false;
				// 			this.last_id = this.last_id+1;
				// 			if(data.data.total<this.last_id*limit){
				// 				this.status = '';
				// 			}
				// 		}else{
				// 			this.mendiandatatype=2
				// 			this.contentText.contentdown='没有数据'
				// 		}
				// 	},
				// 	fail: (data, code) => {
				// 		//console.log('fail' + JSON.stringify(data));
				// 	}
				// });
			},
			selectMendian(id,e){
				uni.setStorage({//缓存配置信息
					key: 'busId',  
					data: id
				})
				uni.setStorage({//缓存配置信息
					key: 'mendian',  
					data: e
				})
				setTimeout(()=>{
					uni.navigateBack({
						delta:1
					})
				},500)
				return
				data.token = uni.getStorageSync("userinfo").token;
				uni.request({
					url: this.configs.webUrl+'/api/user/setmendian',
					data: data,
					success: data => {
						console.log(data.data)
						if (data.data.code==1) {
							uni.showModal({
								title: '温馨提示',
								content: data.data.msg,
								showCancel: false,
								confirmText: "确定",
								success: function (res) {
									uni.navigateBack();
								}
							});
						}else{
							uni.showToast({ title: data.data.msg,icon:"none" });
						}
					},
					fail: (data, code) => {
						//console.log('fail' + JSON.stringify(data));
					}
				});
				
			},
		}
	}
</script>

<style>
	
</style>